package cn.xshi.sys.web;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.xshi.sys.form.SysKnowledgeForm;
import cn.xshi.sys.model.SysKnowledge;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BasePage;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.entity.OauthAccountEntity;
import cn.xshi.common.util.StringUtil;
import cn.xshi.sys.service.SysKnowledgeService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;
/**
 * @Desc 平台知识内容
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/sysKnowledge")
@Api(value = "平台知识内容API",tags = "平台知识内容API",description = "平台知识内容API")
public class SysKnowledgeController extends BaseAction {

	@Resource
	private SysKnowledgeService sysKnowledgeService;

	/**
	 * 查询平台知识内容列表并分页
	 * @param sysKnowledgeForm
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@GetMapping(value="/list")
	@Auth(value = "/sysKnowledge/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	@ApiOperation(value="查询平台知识内容列表并分页", notes="查询平台知识内容列表并分页")
	public BasePage<List<SysKnowledge>> getSysKnowledgeListByCondition(SysKnowledgeForm sysKnowledgeForm,Integer pageNo, Integer pageSize){
		commonHPagerByPageNo(pageNo,pageSize);
		List<SysKnowledge> sysKnowledgeList = sysKnowledgeService.getSysKnowledgeListByCondition(sysKnowledgeForm);
		for(SysKnowledge sysKnowledge : sysKnowledgeList){
			if(!StringUtil.isEmpty(sysKnowledge.getCreateId())){
				OauthAccountEntity createBy = getAccount(sysKnowledge.getCreateId());
				if(null != createBy){
					sysKnowledge.setCreateBy(createBy.getName());
				}
			}
			if(!StringUtil.isEmpty(sysKnowledge.getUpdateId())){
				OauthAccountEntity modifiedBy = getAccount(sysKnowledge.getUpdateId());
				if(null != modifiedBy){
					sysKnowledge.setModifiedBy(modifiedBy.getName());
				}
			}
		}
		PageInfo<SysKnowledge> page = new PageInfo<SysKnowledge>(sysKnowledgeList);
		return outPageBootByPageNo(page,pageNo,pageSize);
	}

	/**
	 * 查询单个平台知识内容
	 * @param id
	 */
	@GetMapping(value="/get/{id}")
	@Auth(value = "/sysKnowledge/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	@ApiOperation(value="查询单个平台知识内容", notes="查询单个平台知识内容")
	public BaseResult getSysKnowledgeById(@PathVariable("id")String id){
		SysKnowledge sysKnowledge = sysKnowledgeService.getSysKnowledgeById(id);
		if(!StringUtil.isEmpty(sysKnowledge.getCreateId())){
			OauthAccountEntity createBy = getAccount(sysKnowledge.getCreateId());
			if(null != createBy){
				sysKnowledge.setCreateBy(createBy.getName());
			}
		}
		if(!StringUtil.isEmpty(sysKnowledge.getUpdateId())){
			OauthAccountEntity modifiedBy = getAccount(sysKnowledge.getUpdateId());
			if(null != modifiedBy){
				sysKnowledge.setModifiedBy(modifiedBy.getName());
			}
		}
		return outDataStr(sysKnowledge);
	}

	/**
	 * 添加
	 * @param sysKnowledge
	 */
	@PostMapping(value="/add")
	@ApiOperation(value="创建单个平台知识内容", notes="创建单个平台知识内容")
	public BaseResult addSysKnowledge(@RequestBody SysKnowledge sysKnowledge){
		int i = 0;
		if(null != sysKnowledge){
			sysKnowledge.setId(toUUID());
			i= sysKnowledgeService.addSysKnowledge(sysKnowledge);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 修改
	 * @param sysKnowledge
	 */
	@PutMapping(value="/update")
	@ApiOperation(value="编辑单个平台知识内容", notes="编辑单个平台知识内容")
	public BaseResult updateSysKnowledge(@RequestBody SysKnowledge sysKnowledge){
		int i = 0;
		if(null != sysKnowledge){
			i= sysKnowledgeService.updateSysKnowledge(sysKnowledge);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 删除
	 * @param id
	 */
	@DeleteMapping(value="/delete")
	@ApiOperation(value="删除平台知识内容", notes="删除平台知识内容")
	public BaseResult delSysKnowledge(String id){
		int i = 0;
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			i= sysKnowledgeService.delSysKnowledge(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 知识库数
	 * @return
	 */
	@GetMapping(value="/count")
	@Auth(value = "/sysKnowledge/count",authenticationType = Auth.AuthorizationType.NOT_REQUIRED_LOGIN)
	@ApiOperation(value="知识库数", notes="知识库数")
	public BaseResult count(){
		int count = sysKnowledgeService.getSysKnowledgeCount(new HashMap<>());
		return outDataStr(count);
	}
}
